/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Supplier;

/**
 *
 * @author Trang
 */
public class SupplierDAO {
    private Connection con;
    public SupplierDAO()
    {
        con = DBFactory.createConnection("onlineshopping");
    }
    
    public List<Supplier> getAllSuppliers() {
        List<Supplier> result = new ArrayList<Supplier>();

        try {
            String sqlst = "SELECT * FROM supplier";
            PreparedStatement ps = con.prepareStatement(sqlst);

            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                Integer idSupplier = rs.getInt("idSupplier");
                String name = rs.getString("name");
                String address = rs.getString("address");
                String description = rs.getString("description");
                String email = rs.getString("email");
                String phone = rs.getString("phone");
                result.add(new Supplier(idSupplier, name, address, description, email, phone));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }
    public boolean InsertSupplier(Supplier supplier) {
        boolean result = false;

        try {
            String sqlst1 = "INSERT INTO supplier(email, name, address, description, phone, others) "
                    + "VALUE(?, ?, ?, ?, ?, ?)";

            PreparedStatement ps = con.prepareStatement(sqlst1);
            ps.setString(1, supplier.getEmail());
            ps.setString(2, supplier.getName());
            ps.setString(3, supplier.getAddress());
            ps.setString(4, supplier.getDescription());
            ps.setString(5, supplier.getPhone());
            ps.setString(6, supplier.getOthers());
//            ps.setDate(9, new java.sql.Date (staff.getDob().getTime()));
            
            ps.executeUpdate();
            result = true;
        } catch (SQLException ex) {
            Logger.getLogger(AuthenticationDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }
    public Supplier getSupplierFromSupplierName(String sName) {
        Supplier result = new Supplier();
        try {
            String sqlst = "SELECT * FROM supplier WHERE name = ?";

            PreparedStatement ps = con.prepareStatement(sqlst);
            ps.setString(1, sName);

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                Integer idSupplier = rs.getInt("idSupplier");
                String name = rs.getString("name");
                String address = rs.getString("address");
                String description = rs.getString("description");
                String email = rs.getString("email");
                String others = rs.getString("others");
                String phone = rs.getString("phone");

                result= new Supplier(idSupplier, name, address, description, email, phone);
                
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }
     public Supplier getSupplierFromSupplierID(Integer sid) {
        Supplier result = new Supplier();
        try {
            String sqlst = "SELECT * FROM supplier WHERE idSupplier = ?";

            PreparedStatement ps = con.prepareStatement(sqlst);
            ps.setInt(1, sid);

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                Integer idSupplier = rs.getInt("idSupplier");
                String name = rs.getString("name");
                String address = rs.getString("address");
                String description = rs.getString("description");
                String email = rs.getString("email");
                String others = rs.getString("others");
                String phone = rs.getString("phone");

                result= new Supplier(idSupplier, name, address, description, email, phone);
                
                
            }
        } catch (SQLException ex) {
            Logger.getLogger(ProductDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return result;
    }
}
